package casestudy.file;

import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class FileTest {
	
	public static void main(String ... v){
		System.out.println("File Test ..");
		
		String src = "File Write and Access TEST ..";
		byte[] by = src.getBytes();
		
		ByteBuffer buf = ByteBuffer.allocate(by.length);
		// directBuffer
		buf.put(by);
		buf.clear();
		
		try {
			FileOutputStream fos = new FileOutputStream("d:\\a.txt");
			FileChannel fch = fos.getChannel();
			
			int cnt = fch.write(buf);
			System.out.println("File Write :" + cnt);
			fch.close();
			
			FileInputStream fis = new FileInputStream("d:\\a.txt");
			FileChannel in = fis.getChannel();
			
			ByteBuffer buf2 = ByteBuffer.allocate((int)in.size());
			in.read(buf2);
			
			byte buffer[] = buf2.array();
			String a = new String(buffer);
			
			System.out.println("Read Data>" + a);
			in.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
